package com.xidian.zhiancloud.service.riskcontrol;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xidian.zhiancloud.dao.InformationMapper;
import com.xidian.zhiancloud.dao.riskcontrol.TRiskTrainingRecordMapper;
import com.xidian.zhiancloud.dao.riskcontrol.TRiskTrainingStudentsMapper;
import com.xidian.zhiancloud.dao.riskcontrol.TtrainingRiskStatusMapper;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.Information;
import com.xidian.zhiancloud.model.riskcontrol.TRiskTrainingRecordList;
import com.xidian.zhiancloud.model.riskcontrol.TRiskTrainingStudentsList;
import com.xidian.zhiancloud.model.riskcontrol.TrainingRiskStatusList;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 风险管控-风险培训
 */
@Service
public class TtrainingRiskStatusService extends
        ServiceImpl<TtrainingRiskStatusMapper, TrainingRiskStatusList> {

    @Autowired
    TtrainingRiskStatusMapper ttrainingRiskStatusMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    TRiskTrainingStudentsMapper tRiskTrainingStudentsMapper;

    @Autowired
    TRiskTrainingRecordMapper tRiskTrainingRecordMapper;

    @Autowired
    private InformationMapper informationMapper;

    public Page<TrainingRiskStatusList> getTtrainingRiskStatusList(BaseReqQuery reqdata, Long companyId, String state){
        PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
        Page<TrainingRiskStatusList> pagedata =ttrainingRiskStatusMapper.getTtrainingRiskStatusList(reqdata.getSearchKey(),companyId,state);
        return pagedata;
    }

    public int addTtrainingRiskStatus(TrainingRiskStatusList trainRiskSta){
        ttrainingRiskStatusMapper.addTtrainingRiskStatus(trainRiskSta);
        Long id =  trainRiskSta.getId();

        List<TRiskTrainingStudentsList> traiStudenList = new
                ArrayList<>();
        String[] userId= request.getParameter("userId").split(",");
        String[] orgId= request.getParameter("orgId").split(",");
        String[] jobId= request.getParameter("jobId").split(",");
        for (int i = 0; i < userId.length; i++) {
            TRiskTrainingStudentsList traiStuden = new TRiskTrainingStudentsList();
                traiStuden.setCompanyId(trainRiskSta.getCompanyId());
                traiStuden.setTrainingRiskStatusId(id.intValue());
                traiStuden.setUserId(Integer.valueOf(userId[i]));
                traiStuden.setOrgId(orgId[i]);
                traiStuden.setJobId(jobId[i]);
                traiStudenList.add(traiStuden);
        }
        return tRiskTrainingStudentsMapper.insertTrainingStudents(traiStudenList);
    }
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public int updateTtrainingRiskStatus(String id,TrainingRiskStatusList trainRiskSta){
        int res = ttrainingRiskStatusMapper.updateTtrainingRiskStatus
            (id,trainRiskSta);

        List<TRiskTrainingStudentsList> traiStudenList = new
            ArrayList<>();
        String[] userId= request.getParameter("userId").split(",");
        String[] orgId= request.getParameter("orgId").split(",");
        String[] jobId= request.getParameter("jobId").split(",");
        for (int i = 0; i < userId.length; i++) {
            TRiskTrainingStudentsList traiStuden = new TRiskTrainingStudentsList();
            traiStuden.setCompanyId(trainRiskSta.getCompanyId());
            traiStuden.setTrainingRiskStatusId(Integer.valueOf(id));
            traiStuden.setUserId(Integer.valueOf(userId[i]));
            traiStuden.setOrgId(orgId[i]);
            traiStuden.setJobId(jobId[i]);
            traiStudenList.add(traiStuden);
        }
        return tRiskTrainingStudentsMapper.insertTrainingStudents(traiStudenList);
    }
    
    public List<TrainingRiskStatusList> getByTtraRStatus(String id){
        return ttrainingRiskStatusMapper.getByTtraRStatus(id);
    }

    public int insertTrainRecord(TRiskTrainingRecordList data){
        return tRiskTrainingRecordMapper.insertTrainRecord(data);
    }

    public Page<TrainingRiskStatusList> getExportTtrainingRiskStatusList
            (Long companyId, String id){
        List<Object> list=getObjeList(id);
        return ttrainingRiskStatusMapper.getExportTtrainingRiskStatusList
                (companyId,list);
    }

    public List<Object> getObjeList(String ids) {
        List<Object> list = new ArrayList<Object>();
        String[] str = ids.split(",");
        long[] strArrNum = (long[]) ConvertUtils.convert(str,long.class);
        for (Long id : strArrNum) {
            list.add(id);
        }
        return list;
    }
    
    public int deleteByTtraRStatus(String id){
        List<String> list=getList(id);
        int res = ttrainingRiskStatusMapper.deleteByTtraRStatus(list);
        if(res>0){
            tRiskTrainingStudentsMapper.deleteByByTraRiskStaId(id);
        }
        return res;
    }

    /**
     * id放入list
     * @param id id(多个已逗号分隔)
     * @return List集合
     */
    public List<String> getList(String id) {
        List<String> list = new ArrayList<String>();
        String[] str = id.split(",");
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        return list;
    }
    
    public int publishTrainRecord(Long userId,String id){
        int res = ttrainingRiskStatusMapper.publishTrainRecord(id);
        TrainingRiskStatusList trai = ttrainingRiskStatusMapper.getByIds(id);
        List<TRiskTrainingStudentsList> studentsList= 
            tRiskTrainingStudentsMapper.getByTraRiskStaId(id);
        if(res!=0){
            for (TRiskTrainingStudentsList stu : studentsList){
                Information info = new Information();
                info.setTitle(String.valueOf(trai.getRiskProject()));
                info.setStatus("0");
                info.setDepartment(String.valueOf(userId));
                info.setDetails(String.valueOf(trai.getRiskProject()));
                info.setSendId(userId);//发送者
                info.setTargetId(Long.valueOf(stu.getUserId()));//接收者
                info.setCreateTime(new Date());
                info.setUpdateTime(new Date());
                informationMapper.insertInfo(info);
            }
        }
        
        return res;
    }
    

}
